package com.supor.aiot.home.ui.device.config;

import android.annotation.SuppressLint;
import android.app.Application;
import android.text.TextUtils;

import com.android.baseconfig.common.utils.GsonUtil;
import com.android.baseconfig.common.utils.Logc;
import com.google.gson.reflect.TypeToken;
import com.supor.aiot.common.app.api.DeviceImpl;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.ProductType;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.helper.GlobalDataCache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

/**
 * @author Xiaozhuang
 * @date 2021/11/11
 * @description NFCDeviceGuideViewModel
 */
public class NFCDeviceGuideViewModel extends BaseViewModel {

    private final MutableLiveData<Result> bindResult = new MutableLiveData<>();

    public MutableLiveData<Result> getBindResult() {
        return bindResult;
    }

    private final MutableLiveData<Boolean> updateResult = new MutableLiveData<>();

    public MutableLiveData<Boolean> getUpdateResult() {
        return updateResult;
    }

    public NFCDeviceGuideViewModel(@NonNull Application application) {
        super(application);
    }

    public void bindNFCDevice(String mac, String modelId) {
        Map<String, String> map = new HashMap<>();
        map.put("productModel", modelId);
        map.put("mac", mac);
        map.put("bindType", "nfc");
        map.put("thirdType", "Supor");
        DeviceImpl.getInstance().bind(map).subscribe(result -> {
            if (result.isSuccess() && result.data != null) {
                bindResult.setValue(new Result.Success(result.data));
            } else {
                bindResult.setValue(new Result.Error("bindNFCDevice failed"));
            }
        }, throwable -> {
            Logc.e("bindNFCDevice: " + throwable);
            bindResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }

    @SuppressLint("CheckResult")
    public void getAllProduct() {
        if (GlobalDataCache.productTypeList == null) {
            DeviceImpl.getInstance().getAllProduct().subscribe(result -> {
                List<ProductType> productTypeList = result.data.rows;
                List<ProductType> tempList = new ArrayList<>();
                for (ProductType productType : productTypeList) {
                    if (productType.Children == null) {
                        continue;
                    }
                    for (ProductType.ProductModel child : productType.Children) {
                        if (TextUtils.isEmpty(child.modelPicture)) {
                            child.modelPicture = productType.modelPicture;
                        }
                        // pid
                        child.productId = child.salsModelName;
                    }
                    tempList.add(productType);
                }
                GlobalDataCache.productTypeList =
                        GsonUtil.getInstance().toObject(GsonUtil.getInstance().toJson(tempList), new TypeToken<List<ProductType>>() {
                        }.getType());
            }, throwable -> {
                Logc.e("accept: " + throwable);
                // ToastUtils.getInstance().showCustomToast("获取产品失败");
            });
        }
    }

    public void updateMac(String newMac, String oldMac) {
        DeviceImpl.getInstance().updateMac(newMac, oldMac, "Supor").subscribe(result -> {
            updateResult.setValue(result.data);
        }, throwable -> {
            Logc.e("updateMac: " + throwable);
        });
    }

}
